package com.karaoke.android.kege.vm

import android.app.FragmentManager
import android.databinding.ObservableArrayList
import android.databinding.ObservableInt
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.apkfuns.logutils.LogUtils
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.domain.DataDictionary
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.R
import com.karaoke.android.kege.fragment.KegeFragments
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.mvvm.FragmentViewModel
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import rx.Observable
import universum.studios.android.fragment.manage.FragmentController
import universum.studios.android.fragment.manage.FragmentRequest
import java.util.concurrent.TimeUnit
import kotlin.properties.Delegates


/**
 * Created by Administrator on 2017/4/5.
 */
class SingerViewModel(fm: FragmentManager) : FragmentViewModel(), AdapterView.OnItemClickListener
{
    val selectedPosition = ObservableInt().apply { set(-1) }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
    {
        LogUtils.e("selected pos $position id $id")
        if (selectedPosition.get() != position)
        {
            val area = areas[position]
            val arg = Bundle()
            arg.putParcelable("area", area)
            controller.clearBackStack()
            controller.newRequest(KegeFragments.SINGER_PAGE)
                    .apply {
                        arguments(arg)
                        allowStateLoss()
                    }
                    .tag("SINGER_PAGE_${area.id}")
                    .transaction(FragmentRequest.REPLACE)
                    .execute()
        }
        selectedPosition.set(position)
    }

    override fun onInitLoad()
    {
        super.onInitLoad()
        if (areas.isNotEmpty()) return
        NetworkTools.getSingerAreas().bindToLifecycle(this).onUI({ initViews(it) }, {})
    }

    val areas = ObservableArrayList<DataDictionary>()
    val menuBindings = ItemBinding.of<DataDictionary>(BR.item, R.layout.menu_list_item)
    private var controller by Delegates.notNull<FragmentController>()

    init
    {
        controller = FragmentController(fm)
        controller.viewContainerId = R.id.spContainer
        controller.factory = KegeFragments()
    }

    override fun onCreate()
    {
        super.onCreate()
    }

    private fun initViews(list: List<DataDictionary>)
    {
        areas.addAll(list)
        if (areas.isNotEmpty())
        {
            Observable.just(selectedPosition.set(0)).bindToLifecycle(this).delay(300, TimeUnit.MILLISECONDS)
            controller.newRequest(KegeFragments.SINGER_PAGE)
                    .apply {
                        val arg = Bundle()
                        arg.putParcelable("area", areas[0])
                        arguments(arg)
                        allowStateLoss()
                    }
                    .tag("SINGER_PAGE_0")
                    .transaction(FragmentRequest.REPLACE)
                    .execute()
        }
    }
}